{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8dde1cf2",
   "metadata": {},
   "source": [
    "* 记录人 ： 曾烨"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7881a23e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.python 使用api模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b737edb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1eac276b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.api账号/通行证\n",
    "API_key = '76-_4m_gpMf7ZJT9IN--sRnBETvlIitx'\n",
    "API_secret = 'A4vJxaa7sQoogmb7QZ_Ce-Rt7nY0gAn9'\n",
    "\n",
    "# 3. 请求方法: post \n",
    "\n",
    "#4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://img.alicdn.com/tfs/TB1A7Kwzq61gK0jSZFlXXXDKFXa-764-1146.png'\n",
    "}\n",
    "\n",
    "#5.发起请求\n",
    "r = requests.post(url = base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1b047688",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "920cde98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647863735,f82283a4-7c0e-4b24-a347-6ffd2c223315',\n",
       " 'time_used': 264,\n",
       " 'faces': [{'face_token': '54aef9bb53f6cebedc9e9ffb147afbc3',\n",
       "   'face_rectangle': {'top': 338, 'left': 142, 'width': 496, 'height': 496}}],\n",
       " 'image_id': 'PqqlcFK4HC1ED1mjK+11yg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2f78b6fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1647863735,f82283a4-7c0e-4b24-a347-6ffd2c223315\",\"time_used\":264,\"faces\":[{\"face_token\":\"54aef9bb53f6cebedc9e9ffb147afbc3\",\"face_rectangle\":{\"top\":338,\"left\":142,\"width\":496,\"height\":496}}],\"image_id\":\"PqqlcFK4HC1ED1mjK+11yg==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b5eae2a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'face_token': '54aef9bb53f6cebedc9e9ffb147afbc3',\n",
       " 'face_rectangle': {'top': 338, 'left': 142, 'width': 496, 'height': 496}}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['faces'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5453d0e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸分析api\n",
    "\n",
    "# return_attributes的所有参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c412ea05",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "# 2.api账号/通行证\n",
    "API_key = '76-_4m_gpMf7ZJT9IN--sRnBETvlIitx'\n",
    "API_secret = 'A4vJxaa7sQoogmb7QZ_Ce-Rt7nY0gAn9'\n",
    "\n",
    "# 3. 请求方法: post \n",
    "\n",
    "#4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'image_url':'https://img.alicdn.com/tfs/TB1A7Kwzq61gK0jSZFlXXXDKFXa-764-1146.png'\n",
    "}\n",
    "\n",
    "#5.发起请求\n",
    "r_analyze = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "147cc75a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 55,\n",
       " 'error_message': 'MISSING_ARGUMENTS: face_tokens',\n",
       " 'request_id': '1647865306,f956178d-e654-4e70-9afe-06c502dd12cc'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8bc9f24d",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "string indices must be integers",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-26-be55a2b408ee>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mface_tokens\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'faces'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'face_token'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: string indices must be integers"
     ]
    }
   ],
   "source": [
    "face_tokens = ['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "aa585c54",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2723a603",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1. 数据处理小魔法 pandas.json_normaliza() 实现数据表格化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f8039555",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>54aef9bb53f6cebedc9e9ffb147afbc3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>338</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>496</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                      0\n",
       "face_token             54aef9bb53f6cebedc9e9ffb147afbc3\n",
       "face_rectangle.top                                  338\n",
       "face_rectangle.left                                 142\n",
       "face_rectangle.width                                496\n",
       "face_rectangle.height                               496"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3b24f2e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 人脸对比\n",
    "## 人脸对比至少需要几张脸？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "365f153a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.api账号/通行证\n",
    "API_key = '76-_4m_gpMf7ZJT9IN--sRnBETvlIitx'\n",
    "API_secret = 'A4vJxaa7sQoogmb7QZ_Ce-Rt7nY0gAn9'\n",
    "\n",
    "# 3. 请求方法: post \n",
    "\n",
    "#4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://img.alicdn.com/tfs/TB1A7Kwzq61gK0jSZFlXXXDKFXa-764-1146.png'\n",
    "}\n",
    "\n",
    "#5.发起请求\n",
    "r = requests.post(url = base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6727221f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'21786cb4c68adddd9e0adccb32f96000'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "9e92b32b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.api账号/通行证\n",
    "API_key = '76-_4m_gpMf7ZJT9IN--sRnBETvlIitx'\n",
    "API_secret = 'A4vJxaa7sQoogmb7QZ_Ce-Rt7nY0gAn9'\n",
    "\n",
    "# 3. 请求方法: post \n",
    "\n",
    "#4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://img.sosohuo.com/uploads/2020/11/19/meizhuang-5.jpg'\n",
    "}\n",
    "\n",
    "#5.发起请求\n",
    "r = requests.post(url = base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "34eae595",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0778829428052fcd5b079e1ae09b90fc'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e86b7587",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "2d4f1145",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647866848,51516628-ac93-48d4-a1df-87907c8b7d0a',\n",
       " 'time_used': 429,\n",
       " 'faces': [{'face_token': '0778829428052fcd5b079e1ae09b90fc',\n",
       "   'face_rectangle': {'top': 144, 'left': 32, 'width': 747, 'height': 747}}],\n",
       " 'image_id': '0brLKjpjEAK9n2p/DPXnFw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "9920dd6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "fd858b1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_secret,img_url):\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    API_key = '76-_4m_gpMf7ZJT9IN--sRnBETvlIitx'\n",
    "    API_secret = 'A4vJxaa7sQoogmb7QZ_Ce-Rt7nY0gAn9'\n",
    "    payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://img.alicdn.com/tfs/TB1A7Kwzq61gK0jSZFlXXXDKFXa-764-1146.png'\n",
    "}\n",
    "    r = requests.post(url = base_url,params = payload)\n",
    "    return r.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bba451bb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a2de98d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6940e323",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7f493e1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebb3c6fc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66be0b7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6c937da",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
